/*
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package botobjects;

import interfaces.BotObject;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;

import tools.StringTools;
import database.DBConnectionFactory;

/**
 * This class represents a user's stats
 * 
 * 
 */
public class Stats implements BotObject {
   /**
    * Returns the stats for the specified user, or all users playing a province
    * 
    * @param args
    * @return a List of Stats objects
    */
   public static List<Stats> getStats(final String... args) {
	  List<Stats> out = new LinkedList<Stats>();
	  PreparedStatement ps = null;
	  ResultSet rs = null;
	  try {
		 if (args.length == 1) {
			ps = DBConnectionFactory.getConnectionHandler().getConnection()
				  .prepareStatement("SELECT * FROM actions WHERE performer = ?");
			ps.setString(1, args[0]);
			rs = ps.executeQuery();
			Map<String, Integer> map = new HashMap<String, Integer>();
			String performer = "";
			while (rs.next()) {
			   if ("".equals(performer)) {
				  performer = rs.getString("performer");
			   }
			   map.put(rs.getString("actiontype"), rs.getInt("amount"));
			}
			out.add(new Stats(performer, map));
		 } else {
			ps = DBConnectionFactory.getConnectionHandler().getConnection().prepareStatement("SELECT * FROM kdprovince");
			rs = ps.executeQuery();
			List<String> users = new LinkedList<String>();
			while (rs.next()) {
			   users.add(rs.getString("mainnick"));
			}

			for (String user : users) {
			   ps = DBConnectionFactory.getConnectionHandler().getConnection()
					 .prepareStatement("SELECT * FROM actions WHERE performer = ?");
			   ps.setString(1, user);
			   rs = ps.executeQuery();
			   Map<String, Integer> map = new HashMap<String, Integer>();
			   String performer = "";
			   while (rs.next()) {
				  if ("".equals(performer)) {
					 performer = rs.getString("performer");
				  }
				  map.put(rs.getString("actiontype"), rs.getInt("amount"));
			   }
			   out.add(new Stats(performer, map));
			}
		 }
	  } catch (final SQLException e) {
		 Logger.getLogger("Error").error("", e);
	  } finally {
		 try {
			rs.close();
		 } catch (final Exception e) {
		 }
		 try {
			ps.close();
		 } catch (final Exception e) {
		 }
	  }
	  return out;
   }

   private String nick = "";

   private Map<String, Integer> statsMap = new HashMap<String, Integer>();

   public Stats(final String nick, final Map<String, Integer> statsMap) {
	  this.nick = nick;
	  this.statsMap = statsMap;
   }

   @Override
   public List<String> getIRCOutput(final int outputType, final String... args) {
	  List<String> out = new LinkedList<String>();
	  if (StringTools.isNullOrEmpty(getNick()) || getStatsMap() == null)
		 return out;
	  for (Entry<String, Integer> e : getStatsMap().entrySet()) {
		 out.add(e.getKey() + ": " + e.getValue());
	  }
	  return out;
   }

   public String getNick() {
	  return nick;
   }

   @Override
   public Map<String, Object> getRPCOutput(final int... includes) {
	  Map<String, Object> out = new HashMap<String, Object>();
	  if (StringTools.isNullOrEmpty(getNick()) || getStatsMap() == null)
		 return out;
	  out.put("Nick", getNick());
	  out.put("Stats", getStats());
	  return out;
   }

   public Map<String, Integer> getStatsMap() {
	  return statsMap;
   }

   @Override
   public boolean saveToDB() {
	  return false;
   }
}
